package com.dapeis.api.controller.collocation.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.ag.core.base.Pager;
import com.ag.core.exception.SysTipsException;
import com.ag.core.exception.VisitLimitException;
import com.ag.core.utils.AgAssert;
import com.ag.dao.PagerHelper;
import com.ag.utils.DateUtils;
import com.ag.utils.NumberUtils;
import com.ag.utils.json.AGsonBuilder;
import com.dapeis.api.controller.RestController;
import com.dapeis.api.controller.collocation.IOrderController;
import com.dapeis.core.convert.pager.OrderPagerSearchConvertCriteria;
import com.dapeis.core.utils.DBConfigUtils;
import com.dapeis.core.utils.RequirementConfig;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.domain.Order;
import com.dapeis.domain.ext.OrderExt;
import com.dapeis.domain.ext.TaskOrderExt;
import com.dapeis.service.OrderService;
import com.dapeis.service.RequirementService;
import com.dapeis.service.TaskService;
import com.dapeis.service.UserService;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

@Controller
@RequestMapping("/dps/order")
public class OrderController extends RestController implements
	IOrderController {
    
    @Resource
    TaskService taskService;

    @Resource
    OrderService orderService;
    
    @Resource
    RequirementService requirementService;

    @Resource
    UserService userService;

    @Override
    public JsonElement getMyOrderList(
	    @RequestParam(required = false) Integer curretPager,
	    HttpServletRequest request) {
	Pager pager = new Pager();
	pager.init(OrderExt.class, new OrderPagerSearchConvertCriteria());
	Example example = new Example(TaskOrderExt.class);
	Criteria criteria = example.createCriteria();
	criteria.andEqualTo(Order.PROPERTY_COLLOCATIONID,
		getThreadLocalCollocationId());
	criteria.andEqualTo(Order.PROPERTY_TYPE,
		StylistConstants.Order.Type.REQUIREMENT);
	example.orderBy(Order.PROPERTY_RECORDCREATETIME).desc();
	pager.setExample(example);
	if (NumberUtils.isValid(curretPager)) {
	    pager.setCurrentPage(curretPager);
	}
	taskService.pager(pager);
	//PagerHelper.pager(pager, taskService.);
	JsonObject json = buildSuccess();
	json.add("pager", AGsonBuilder.create().toJsonTree(pager));
	return json;
    }

    @Override
    public JsonElement detail(@RequestParam Long orderId, HttpServletRequest request) {
	OrderExt orderExt = orderService.getOrderExtMapper().selectByPrimaryKey(orderId);
	AgAssert.notNull(orderExt,SysTipsException.class, "查询不到订单信息orderId:"+orderId);
	AgAssert.isTrue(orderExt.getCollocationId() == getThreadLocalCollocationId(),
		VisitLimitException.class, "无权查看.");
	
	JsonObject json = buildSuccess();
	json.add("requirement",
		AGsonBuilder.create().toJsonTree(orderExt.getRequirement()));
	json.add("order",
		AGsonBuilder.create().toJsonTree(orderExt, Order.class)
			.getAsJsonObject());
	json.add("comment",
		AGsonBuilder.create().toJsonTree(orderExt.getComment()));
	json.add("user",
		AGsonBuilder.create().toJsonTree(orderExt.getUserExt()));
	return json;
    }

}
